package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //1. 校验参数
        if(null == multipartFile || multipartFile.getInputStream().available() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 上传文件
        //2.1 文件名称重复
        String filename = multipartFile.getOriginalFilename();
        filename = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
        //2.2 上传文件
        String path = fileStorageService.uploadHtmlFile("",
                filename, //a.jpg a.jpg
                multipartFile.getInputStream());

        //3. 保存素材数据
        WmMaterial entity = new WmMaterial();
        entity.setCreatedTime(new Date());
        entity.setUrl(path);
        entity.setType((short)0);
        entity.setIsCollection((short)0);
        entity.setUserId(WmThreadLocalUtil.getUser().getId());
        save(entity);

        //4. 构建返回结果
        return ResponseResult.okResult(entity);
    }

    /**
     * 根据条件分页查询素材数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1. 校验参数
        if(null == dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam(); //校验并修正分页参数

        //2. 设置查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        if(null != dto.getIsCollection() && dto.getIsCollection() == 1){
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //设置自己的id 查询自己的素材数据
        WmUser user = WmThreadLocalUtil.getUser();
        if(null != user){
            queryWrapper.eq(WmMaterial::getUserId, user.getId());
        }

        //3. 设置分页参数
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        //4. 设置排序规则
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //5. 分页查询数据
        page(page, queryWrapper);

        //6. 封装返回结果
        PageResponseResult pageResult = new PageResponseResult();
        pageResult.setData(page.getRecords());
        pageResult.setTotal((int) page.getTotal());
        pageResult.setSize(dto.getSize());
        pageResult.setCurrentPage(dto.getPage());

        return pageResult;
    }
}
